package com.example.demo.controller;


import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import com.example.demo.utils.AjaxJson;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author
 * @since 2025-11-10
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    /**
     * 系统登陆
     * 用户通过输入用户名和密码并选择自己的角色进行登陆，系统根据登陆信息调出相应的功能模块。
     */
    @RequestMapping("/login")
    public AjaxJson login(@RequestParam String username, @RequestParam String password, @RequestParam Integer roleId, HttpServletRequest request) {
        AjaxJson login = userService.login(username, password, roleId);
        if (login.code == 200) {
            // 将用户信息存储到Session中，而不是请求属性中
            request.getSession().setAttribute("user", login.getData(User.class));
            System.out.println("登陆成功" + login.getData(User.class));
        }
        return login;
    }

    /**
     * 修改密码
     */
    @RequestMapping("/updatePassword")
    public AjaxJson updatePassword(@RequestParam String oldPassword, @RequestParam String newPassword, HttpServletRequest request) {
        // 从Session中获取用户信息
        AjaxJson ajaxJson = userService.updatePassword(oldPassword, newPassword, (User) request.getSession().getAttribute("user"));
        this.logout(request);
        return ajaxJson;
    }

    /**
     * 管理员批准用户为正式用户
     */
    @RequestMapping("/approve")
    public AjaxJson approve(@RequestParam Integer id) {
        return userService.approve(id);
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    public AjaxJson register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 退出登陆
     */
    @RequestMapping("/logout")
    public AjaxJson logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        System.out.println("退出登陆");
        return AjaxJson.getSuccess("退出登陆成功");
    }

    /**
     * 添加用户
     */
    @RequestMapping("/add")
    public AjaxJson add(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 删除用户
     */
    @RequestMapping("/delete")
    public AjaxJson delete(@RequestParam Integer id) {
        return userService.removeById(id) ? AjaxJson.getSuccess("删除成功") : AjaxJson.getError("删除失败");
    }

    /**
     * 更新用户
     */
    @RequestMapping("/update")
    public AjaxJson update(@RequestBody User user) {
        System.out.println("更新用户" + user);
        // 检查用户ID是否存在
        if (user.getUserId() == null) {
            return AjaxJson.getError("用户ID不能为空");
        }
        return userService.updateById(user) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
    }

    /**
     * 查询所有用户
     */
    @RequestMapping("/query")
    public AjaxJson query() {
        return AjaxJson.getPageData(userService.count(), userService.list().stream().peek(user -> user.setPassword("")));
    }

    /**
     * 根据ID查询用户
     */
    @RequestMapping("/query/id")
    public AjaxJson query(@RequestParam Integer id) {
        User user = userService.getById(id);
        if (user != null) {
            user.setPassword("");
        }
        return AjaxJson.getSuccessData(user);
    }
}